package com.pospi.pospihold.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.pospi.pospihold.App;
import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.CashierMsgDto;
import com.pospi.pospihold.fragment.CashierFragment;
import com.pospi.pospihold.fragment.MyFragment;
import com.pospi.pospihold.fragment.StatisticsFragment;
import com.pospi.pospihold.fragment.VipFragment;
import com.pospi.pospihold.ui.login_ui.LoginActivity;
import com.pospi.pospihold.util.PareseJson;

import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity {

    @Bind(R.id.tabHost)//时间1466564667295

            FragmentTabHost tabHost;
    List<String> tab_texts = Arrays.asList("收银台", "会员", "统计", "我的");
    List<Integer> tab_icons = Arrays.asList(R.drawable.syt_selector, R.drawable.vip_selector,
            R.drawable.count_selector, R.drawable.my_selector);

    private boolean islogin;
    // 定义一个变量，来标识是否退出
    private boolean isExit = false;

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };


    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        judgeWheter();
        ButterKnife.bind(this);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);


        tabHost.setup(this, getSupportFragmentManager(), R.id.container);
        tabHost.getTabWidget().setDividerDrawable(null);
        tabHost.addTab(tabHost.newTabSpec(tab_texts.get(0)).setIndicator(getTabItemView(0)), CashierFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(tab_texts.get(1)).setIndicator(getTabItemView(1)), VipFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(tab_texts.get(2)).setIndicator(getTabItemView(2)), StatisticsFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(tab_texts.get(3)).setIndicator(getTabItemView(3)), MyFragment.class, null);

        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                View activity = drawerLayout.getChildAt(0);
                ViewHelper.setTranslationX(activity, drawerView.getMeasuredWidth() * slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        int which = getSharedPreferences("islogin", MODE_PRIVATE).getInt("which", 0);
        List<CashierMsgDto> cashierMsgDtos = new PareseJson().parese(getSharedPreferences("cashierMsgDtos", Context.MODE_PRIVATE).getString("cashierMsgDtos", ""));

        App.setCashierMsgDto(cashierMsgDtos.get(which));
        SharedPreferences sharedPreferences = getSharedPreferences("IsPrinter", MODE_PRIVATE);
        App.IsPrinter = sharedPreferences.getBoolean("IsPrinter", true);


    }

    public void openLeftDrawer() {
        drawerLayout.openDrawer(Gravity.LEFT);
    }

    //首先判断是否需要登录
    public void judgeWheter() {
        SharedPreferences preferences = getSharedPreferences("islogin", MODE_PRIVATE);
        islogin = preferences.getBoolean("islogin", false);
        if (!islogin) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            String value = getSharedPreferences("Token", MODE_PRIVATE).getString("value", "");
            String[] names = value.split("\\,");
            String deviceNo = names[2];//得到token的值

        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index) {
        View view = getLayoutInflater().inflate(R.layout.item_tab_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.item_icon);
        imageView.setImageResource(tab_icons.get(index));
        TextView textView = (TextView) view.findViewById(R.id.item_text);
        textView.setText(tab_texts.get(index));
        return view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
//        if (!isExit) {
//            isExit = true;
//            Toast.makeText(getApplicationContext(), "再按一次退出程序",
//                    Toast.LENGTH_SHORT).show();
//            // 利用handler延迟发送更改状态信息
//            mHandler.sendEmptyMessageDelayed(0, 2000);
//        } else {
//            App.killAllActivities();
//        }
    }
}